package com.hk.yajin.function.videos.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.members.activity.RechargeGlodActivity;
import com.hk.yajin.function.videos.adapter.BuySeriesVideoQuickAdapter;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.DataListModel;
import com.hk.yajin.model.Series;
import com.hk.yajin.model.Video;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;
import com.hk.yajin.view.PopupWindows;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BuySeriesVideoActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_gold)
    TextView tvGold;
    @BindView(R.id.tv_my_video)
    TextView tvBuy;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    protected List<Series> dataList = new ArrayList<>();
    protected RecyclerView mRecyclerView;
    protected BaseQuickAdapter mQuickAdapter;
    private Video fromItemVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_series_video);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void httpLoadSeries() {
        showLoadingView();
        Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
        reqParam.put("te_id", fromItemVideo.getTe_id());
        HttpRequestUtils.post(true, UrlConstants.FUNCTION_LOAD_SERIES_VIDEO, reqParam, false, new HttpCallBack() {
            @Override
            public void onSucced(String data) {
                hideLoadingView();
                DataListModel<Series> model = FastjsonUtils.getBeanObject(data, new TypeReference<DataListModel<Series>>() {
                });
                dataList.addAll(model.getData());
                mQuickAdapter.setNewData(dataList);
            }

            @Override
            public void onBadHttp(String tip) {
                hideLoadingView();
                showShortToast(tip);
            }

            @Override
            public void onErrorCode(NewWorkBean bean) {
                hideLoadingView();
                showShortToast(bean.getMsg());

            }
        });
    }

    private void initData() {
        String dataString = getIntent().getStringExtra(VIDEO);
        fromItemVideo = FastjsonUtils.getBeanObject(dataString, Video.class);

        if (fromItemVideo != null) {
            tvGold.setText(fromItemVideo.getV_gold() + "/月");
            httpLoadSeries();
        } else {
            showShortToast("获取数据失败");
            finish();
        }

    }


    private void initView() {
        initToolBar(R.string.seriesvideo);
        initAdapter();
        initRecyclerView();
    }

    protected void initAdapter() {
        mQuickAdapter = new BuySeriesVideoQuickAdapter(dataList, mContext);
        mQuickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mQuickAdapter.isFirstOnly(true);

    }

    protected void initRecyclerView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mQuickAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                showShortToast("请先订购该系列的视频哦");
            }
        });

    }

    @OnClick(R.id.tv_my_video)
    public void onViewClicked() {
        showPopupWindows();
    }


    /**
     * 弹出菜单
     */
    private void showPopupWindows() {
        new PopupWindows(mContext, getString(R.string.buy_sriers_confirm), fromItemVideo.getV_gold(), tvBuy, new PopupWindows.PopOnClickListener() {
            @Override
            public void onClickYes() {
                httpBuySeriesVideo();
            }

            @Override
            public void onClickNo() {

            }
        });
    }

    private void httpBuySeriesVideo() {
        showLoadingView();
        Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
        reqParam.put("te_id", fromItemVideo.getTe_id());
        HttpRequestUtils.post(false, UrlConstants.FUNCTION_BUY_SERIES_VIDEO, reqParam, false, new HttpCallBack() {
            @Override
            public void onSucced(String data) {
                hideLoadingView();
                showShortToast("购买成功");
                VideoDetailActivity.starItSelf(BuySeriesVideoActivity.this, VideoDetailActivity.class);
            }

            @Override
            public void onBadHttp(String tip) {
                hideLoadingView();
                showShortToast(tip);
            }

            @Override
            public void onErrorCode(NewWorkBean bean) {
                hideLoadingView();
                if (bean.getCode() == 102) {
                    showShortToast("金币不足，请先充值");
                    startActivity(RechargeGlodActivity.class);
                } else {
                    showShortToast(bean.getMsg());
                }
            }
        });
    }
}
